package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import com.jt.vo.PageResult;
import com.jt.vo.SysResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/findAll")
    public List<User> findAll(){
        return userService.findAll();
    }
    @PostMapping("/login")
    public SysResult login(@RequestBody User user){

        String token = userService.findUserByUP(user);
        if(token == null || "".equals(token)){
        //表示用户名和密码错误
        return SysResult.fail();
    }
    //表示用户名和密码正确,返回秘钥信息
        return SysResult.success(token);
    }
    @GetMapping("/list")
    public SysResult userList(PageResult pageResult){
        pageResult = userService.getUserListByPage(pageResult);
        return SysResult.success(pageResult);
    }
    @PutMapping("/status/{id}/{status}")
    public SysResult changById(User user){
        int i = userService.changById(user);
        if(i != 1) return SysResult.fail();
        return SysResult.success();
    }
    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        int i = userService.addUser(user);
        return SysResult.success();
    }
    @GetMapping("/{id}")
    public SysResult findUserById(@PathVariable Integer id){
        User user = userService.findUserById(id);
        return SysResult.success(user);
    }
    @PutMapping("/updateUser")
    public SysResult updateUser(@RequestBody User user){
        userService.updateUser(user);
        return SysResult.success();
    }
    @DeleteMapping("/{id}")
    public SysResult deleteById(@PathVariable Integer id){
        userService.deleteUserById(id);
        return SysResult.success();
    }

}
